package com.xb.cloud.usercenter.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.product.service.client.SystemAddressService;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;
import com.xb.cloud.user.service.client.EndUserAddressService;
import com.xb.cloud.user.service.client.bo.EndUserAddressBo;
import com.xb.cloud.usercenter.service.UserAddressApiService;

@Service
public class UserAddressApiServiceImpl implements UserAddressApiService {

	@Resource
	EndUserAddressService endUserAddressService;
	
	@Resource
	SystemAddressService systemAddressService;

	@Override
	public RtnInfo createtUserAddress(EndUserAddressBo endUserAddressBo) {
		int createtUserAddress = endUserAddressService.createtUserAddress(endUserAddressBo);
		return ReturnUtils.success(createtUserAddress);
	}

	@Override
	public RtnInfo getUserAddressList(Long companyId, Long endUserId) {
		List<EndUserAddressBo> userAddressList = endUserAddressService.getUserAddressListByUserId(companyId, endUserId);
		for (int index = 0;index<userAddressList.size();index++) {
			EndUserAddressBo endUserAddressBo = userAddressList.get(index);
			if(endUserAddressBo.getIsDefault()){
				userAddressList.remove(index);
				userAddressList.add(0, endUserAddressBo);
			}
		}
		return ReturnUtils.success(userAddressList);
	}

	@Override
	public RtnInfo updateUserAddressById(EndUserAddressBo endUserAddressBo) {
		int updated = endUserAddressService.updateUserAddressById(endUserAddressBo);
		return ReturnUtils.success(updated);
	}

	@Override
	public RtnInfo deleteUserAddressById(Long id) {
		EndUserAddressBo endUserAddressBo = endUserAddressService.getUserAddressById(id);
		if (endUserAddressBo == null) {
			ReturnUtils.error(null);
		}
		endUserAddressBo.setIsDeleted(true);
		int deleted = endUserAddressService.deleteUserAddressById(id);
		return ReturnUtils.success(deleted);
	}

	/*private void updateDefaultAddressIfExist(EndUserAddressBo endUserAddressBo) {
		if (endUserAddressBo.getIsDefault()) {
			// 新增地址为默认地址，修改所有地址为非默认
			Long companyId = endUserAddressBo.getCompanyId();
			Long endUserId = endUserAddressBo.getEndUserId();
			EndUserAddressBo defaultAddress = endUserAddressService.getDefaultAddress(companyId, endUserId);
			if (defaultAddress != null && !defaultAddress.getId().equals(endUserAddressBo.getId())) {
				defaultAddress.setIsDefault(false);
				endUserAddressService.updateUserAddressById(defaultAddress);
			}
		}
	}*/

	@Override
	public RtnInfo getProvinces(Long companyId) {
		ServiceReturnInfo serviceReturnInfo = systemAddressService.getProvinces(companyId);
		if(ServiceReturnUtils.isSuccess(serviceReturnInfo)){
			return ReturnUtils.success(serviceReturnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	@Override
	public RtnInfo getCitys(Long provinceId) {
		ServiceReturnInfo serviceReturnInfo = systemAddressService.getCitys(provinceId);
		if(ServiceReturnUtils.isSuccess(serviceReturnInfo)){
			return ReturnUtils.success(serviceReturnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	@Override
	public RtnInfo getCountys(Long cityId) {
		ServiceReturnInfo serviceReturnInfo = systemAddressService.getCountys(cityId);
		if(ServiceReturnUtils.isSuccess(serviceReturnInfo)){
			return ReturnUtils.success(serviceReturnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	@Override
	public RtnInfo getAreas(Long countyId) {
		ServiceReturnInfo serviceReturnInfo = systemAddressService.getAreas(countyId);
		if(ServiceReturnUtils.isSuccess(serviceReturnInfo)){
			return ReturnUtils.success(serviceReturnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

}
